//NumberPanel.java
//Timothy Grant
//CS1410
//Calculator 1410
//Final Version

package calculator1410;

import calculatorio.CalculatorExpression;
import java.awt.Font;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public abstract class NumberPanel extends JPanel {

    JTextArea expressionArea;
    private JScrollPane expressionSPane;
    private JButton equalButton;
    private JButton clearButton;
    private JButton helpButton;
    private JButton saveButton;
    private JButton loadButton;
    private ClearButtonHandler clearHandler;
    private HelpButtonHandler helpHandler;
    private EqualButtonHandler equalHandler;
    private SaveButtonHandler saveHandler;
    private LoadButtonHandler loadHandler;
    private CalculatorExpression IOFile;

    public JPanel createPanel(String name) {
        String panelName = name;
        JPanel newPanel = new JPanel();
        newPanel.setLayout(null);
        clearHandler = new ClearButtonHandler(this);
        helpHandler = new HelpButtonHandler(this);
        equalHandler = new EqualButtonHandler(this);
        saveHandler = new SaveButtonHandler(this);
        loadHandler = new LoadButtonHandler(this);

        //create panel elements
        expressionArea = new JTextArea("Please enter your "
                + panelName + " Number expression...");
        expressionArea.setLocation(0, 0);
        expressionArea.setSize(360, 150);
        expressionArea.setEditable(true);
        expressionArea.setLineWrap(true);
        expressionArea.setWrapStyleWord(false);

        expressionSPane = new JScrollPane(expressionArea);
        expressionSPane.setLocation(20, 20);
        expressionSPane.setSize(310, 150);
        expressionSPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        newPanel.add(expressionSPane);

        //clear button
        clearButton = new JButton("clear");
        clearButton.setSize(70, 30);
        clearButton.setLocation(100, 190);
        Font oldFont = clearButton.getFont();
        Font newFont = new Font(oldFont.getFontName(), Font.BOLD, 14);
        clearButton.setFont(newFont);
        clearButton.addActionListener(clearHandler);
        newPanel.add(clearButton);

        //equal button
        equalButton = new JButton("=");
        equalButton.setSize(70, 30);
        equalButton.setLocation(180, 190);
        oldFont = equalButton.getFont();
        newFont = new Font(oldFont.getFontName(), Font.BOLD, 18);
        equalButton.setFont(newFont);
        equalButton.addActionListener(equalHandler);
        newPanel.add(equalButton);


        //help button
        helpButton = new JButton("i");
        helpButton.setSize(50, 30);
        helpButton.setLocation(20, 240);
        oldFont = helpButton.getFont();
        newFont = new Font(oldFont.getFontName(), Font.BOLD + Font.ITALIC, 14);
        helpButton.setFont(newFont);
        newPanel.add(helpButton);
        helpButton.addActionListener(helpHandler);

        //save button
        saveButton = new JButton("save");
        saveButton.setSize(60, 30);
        saveButton.setLocation(200, 240);
        oldFont = saveButton.getFont();
        newFont = new Font(oldFont.getFontName(), Font.BOLD, 10);
        saveButton.setFont(newFont);
        newPanel.add(saveButton);
        saveButton.addActionListener(saveHandler);

        //load button
        loadButton = new JButton("load");
        loadButton.setSize(60, 30);
        loadButton.setLocation(270, 240);
        oldFont = loadButton.getFont();
        newFont = new Font(oldFont.getFontName(), Font.BOLD, 10);
        loadButton.setFont(newFont);
        newPanel.add(loadButton);
        loadButton.addActionListener(loadHandler);

        return newPanel;
    }

    //@Override
    void clear() {
        expressionArea.setText("");
        expressionArea.repaint();
    }

    abstract void toggleInfo();

    abstract void equals();

    abstract void newEquals();

    protected void save() throws IOException {
        IOFile = new CalculatorExpression(this.expressionArea.getText());
        IOFile.save2File();
    }

    protected void load() throws IOException {
        CalculatorExpression.loadFromFile();
        expressionArea.setText(IOFile.toString());
    }
}
